
#include "app_main.h"
#include "app_key.h"
#include "app_config.h"
#include "app_sys.h"
#include "app_pwr.h"
#include <stdint.h>
#include "user_config.h"

/***
 * KEY
 */

#define KEY_Pin GPIO_PIN_15
#define KEY_GPIO_Port GPIOA

#ifndef KEY_LONG_PRESS_TH_MS
#define KEY_LONG_PRESS_TH_MS    3000
#endif

/// @brief 定时器周期定义（单位：ms）
#define TIMER_PERIOD_MS 1

/// @brief 时间阈值（单位：ms/周期数）
#define TIME_1S (1000 / TIMER_PERIOD_MS)
#define TIME_2S (2000 / TIMER_PERIOD_MS)
#define TIME_3S (3000 / TIMER_PERIOD_MS)

static uint32_t timer_count = 0;

KeyPressData app_key = 
{
    .last_interrupt_time = 0,                        // 初始中断触发时间
    .key_press_start = 0,                            // 初始按下时间
    .last_short_press_time = 0,                      // 初始最后短按时间
    .key_press_count = 0,                            // 初始短按计数
    .is_pressed = 0,                                 // 初始未按下
    .is_long_press = 0,                              // 初始未检测到长按
    .key_state = KEY_IDLE,                           // 假设 KEY_IDLE 表示空闲状态
    .long_press_threshold = KEY_LONG_PRESS_TH_MS,    // 长按阈值，单位ms
    .debounce_time = 40                              // 去抖时间设为 40 ms
};


static void app_key_handle(KeyPressState state)
{
    KeyPressState sta = state;
    switch (sta)
    {
    case KEY_SHORT_PRESS:
        {
            if(app_key.is_times_task_6)
            {
                app_key.is_times_task_6 = 0;
                app_key.key_press_count = 0;
            }
            app_key.key_state = KEY_IDLE;
            app_tra_key_test_stop();
        }
        break;
    case KEY_PRESS_PWR_SWITCH:
        {
            app_key.key_state = KEY_IDLE;
            app_key.key_press_count = 0;
            if(is_power_on())
            {
                app_power_off();
            }
            else
            {
                app_power_on();
            }
        }
        break;
    case KEY_PRESS_SIX_TIMES:
        {
            app_key.key_state = KEY_IDLE;
            if(pwr_state == PWR_ON)
            {
                app_config_mode(WORK_TRA_KEY_TEST_MODE);
            }            
        }
        break;
    default:
        break;
    }
} 


void app_key_timeout(void)
{
    uint32_t current_time = sys_get_tick();
    
    // 检查按键次数超时（5秒 = 5000ms）
    if (app_key.key_press_count > 0 && 
        (current_time - app_key.last_short_press_time) >= 5000)
    {
        app_key.key_press_count = 0; // 清零计数
    }
    
    if (!app_key.is_pressed)  // 如果按键释放则停止计时
    {
        app_key.is_tim_cb_wait_clear = 0;
        timer_count = 0;
        return;
    }

    timer_count++;

    if(timer_count >= KEY_PRESS_PWR_SWITCH_TH_MS && app_key.is_tim_cb_wait_clear)
    {
        app_key.is_tim_cb_wait_clear = 0;
        app_key_handle(KEY_PRESS_PWR_SWITCH);
    }
}


void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    uint32_t current_time = sys_get_tick();

    // 针对 KEY_Pin 的按键逻辑
    if (GPIO_Pin == KEY_Pin)
    {
        // 去抖处理
        if ((current_time - app_key.last_interrupt_time) < app_key.debounce_time)
            return;

        app_key.last_interrupt_time = current_time;

        if (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_RESET) // 按下
        {     
            app_key.is_pressed = 1;
            app_key.key_press_start = current_time;
            app_key.is_tim_cb_wait_clear = 1;
        }
        else // 释放
        {
            app_key.is_pressed = 0;

            // if(app_key.is_tim_cb_wait_clear)    //这段代码暂留
            // {
            //     app_key.is_tim_cb_wait_clear = 0; 
            //     app_key.is_long_press = 0;
			// 	app_key.key_press_count = 0;
            //     app_key.key_state = KEY_IDLE;
            //     return;
            // }

            uint32_t press_duration = current_time - app_key.key_press_start;

            if (press_duration >= app_key.long_press_threshold) // 长按
            {
                app_key.is_long_press = 1;
				app_key.key_press_count = 0;
                app_key.key_state = KEY_LONG_PRESS;
                // app_key_handle(KEY_LONG_PRESS);
            }
            else // 短按
            {
                app_key.is_long_press = 0;
                app_key.key_press_count++;
                app_key.last_short_press_time = current_time; // 记录最后一次短按时间
                app_key.key_state = (app_key.key_press_count >= 6)
                                    ? KEY_PRESS_SIX_TIMES
                                    : KEY_SHORT_PRESS;
 
                app_key_handle(KEY_SHORT_PRESS);

                if (app_key.key_press_count >= 6)
                {
                    app_key.is_times_task_6 = 1;
                    app_key.key_press_count = 0; // 重置多次按下计数
                    app_key_handle(KEY_PRESS_SIX_TIMES);
                }
                    
            }
        }
    }
}






